﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using IrisMembership.DataLayer;
using IrisMembership.ServiceLayer;
using Newtonsoft.Json;

namespace IrisMembership.Web.Controllers
{
    [RoutePrefix("permission")]
    public class PermissionController : Controller
    {
        private readonly IIrisDbContext _dbContext;
        private readonly IRoleService _roleService;
        private readonly IPermissionService _permissionService;

        public PermissionController(IIrisDbContext dbContext, IRoleService roleService, IPermissionService permissionService)
        {
            _dbContext = dbContext;
            _roleService = roleService;
            _permissionService = permissionService;
        }

        public ActionResult Index()
        {
            return PartialView();
        }

        [Route("changepermissions")]
        public ActionResult ChangePermissions()
        {
            return PartialView();
        }


        [Route("changepermissions/{id:int}/s")]
        public async Task<ActionResult> ChangePermissions(int id, string s)
        {
            var data =
                            await
                                JsonConvert.SerializeObjectAsync(await _roleService.GetUserRolesAndPermissionsAsync(this.GetUser().Information.UserId), Formatting.Indented,
                                    new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
            return
                Content(data);
        }

        [Route("getrolepermissions/{roleId:int}")]
        public ActionResult ChangePermissions(int roleId)
        {
            var data =

                                JsonConvert.SerializeObject(_permissionService.GetRolePermissions(roleId), Formatting.Indented,
                                    new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
            return Content(data);
        }



    }
}